﻿using CustomUIControls;
using OrderSystem.Common;
using OrderSystem.DataAccess;
using OrderSystem.SystemFramework;
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Windows.Forms;
using System.Xml;
using Telewave.SoftwareRegister;

namespace OrderSystem
{
    public partial class FormMain : Form
    {
        private FrmRegister frmRegister;
        public FormMain()
        {
            InitializeComponent();
            this.taskbarNotifier3 = new TaskbarNotifier();
            this.taskbarNotifier3.SetBackgroundBitmap(new Bitmap(Application.StartupPath + "\\picture\\skin3.bmp"), Color.FromArgb(255, 0, 255));
            this.taskbarNotifier3.SetCloseBitmap(new Bitmap(Application.StartupPath + "\\picture\\close.bmp"), Color.FromArgb(255, 0, 255), new Point(280, 57));
            this.taskbarNotifier3.TitleRectangle = new Rectangle(150, 57, 125, 28);
            this.taskbarNotifier3.ContentRectangle = new Rectangle(75, 92, 215, 55);
            this.taskbarNotifier3.ContentClick += new EventHandler(this.taskbarNotifier3_ContentClick);
        }

        private void FormMain_Load(object sender, EventArgs e)
        {
            //加密注册验证
            try
            {
                SoftwareRegistryManager.Instance.DoStartProgram = StartLoad;
                SoftwareRegistryManager.Instance.DoStopProgram = StopLoad;
                SoftwareRegistryManager.Instance.DoExceptionHandle = RegistryException;
                //检查软件是否注册
                SoftwareRegistryManager.Instance.CheckIsLegalUse();

            }
            catch (Exception ex)
            {
                MessageBox.Show("注册信息初始化异常：" + ex.InnerException.Message);
            }

        }
        private void StartLoad(string message)
        {
            //继续程序加载
            this.LoadData();
           
        }

        private void StopLoad(string message)
        {
            //关闭程序功能
            this.ShowFrmRegister();
            this.Close();
        }

        private void ShowFrmRegister()
        {
            if (frmRegister == null || frmRegister.IsClosed)
            {
                frmRegister = new FrmRegister();
            }
            frmRegister.ShowDialog();
        }
        private void RegistryException(Exception ex)
        {
            DialogResult result = MessageBox.Show("获取软件注册信息异常" + ex.Message);
            if (result == DialogResult.OK)
            {
                this.ShowFrmRegister();
            }
        }

        private void LoadData()
        { 

            //if (DateTime.Now.Date.CompareTo(FormMain.s_TERMINATE_DATE) >= 0)
            //{
            //    if (FormMain.s_isProbation)
            //    {
            //        MessageBox.Show("试用期限已到！如需继续使用，请与软件开发商联系。", "提示！", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            //    }
            //    else
            //    {
            //        MessageBox.Show("I/O 错误，导致获得应用程序必要信息的配置文件读取错误，此程序可能不能正常运行\r\n可能是有人 更改 或 删除 了此程序安装路径的文件", "错误！", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            //    }
            //}
            this.LogoutOperation();
            this.statusLabelStatus.Text = "正在设置路径...";
            AppSettings.FilePath = Application.StartupPath;
            this.statusLabelStatus.Text = "正在获取背景图片...";
            try
            {
                string appSettings = AppSettings.GetAppSettings("backgroundPicture");
                if (appSettings != "")
                {
                    this.BackgroundImage = Image.FromFile(appSettings);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + "\r\n\r\n获取背景图片时出现错误", "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            this.statusLabelStatus.Text = "正在获取数据库信息......";
            try
            {
                AppSettings.ConnectionString = AppSettings.GetAppSettings("connectionString");
            }
            catch (XmlException ex2)
            {
                MessageBox.Show(ex2.Message + "\r\n\r\n配置文件出现错误", "错误", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
            catch (IOException ex3)
            {
                MessageBox.Show(ex3.Message + "\r\n\r\n配置文件出现问题 或者 丢失", "错误", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            this.isSelfMachine = true;
            this.statusLabelStatus.Text = "正在获取窗体设置信息......";
            Thread.Sleep(180);
            try
            {
                this.Text = AppSettings.GetAppSettings("mainFormCaption");
            }
            catch (XmlException ex2)
            {
                MessageBox.Show(ex2.Message + "\r\n\r\n配置文件出现错误", "错误", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
            catch (IOException ex3)
            {
                MessageBox.Show(ex3.Message + "\r\n\r\n配置文件出现问题 或者 丢失", "错误", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            this.statusLabelStatus.Text = "";
            this.menuLogin_Click(null, null);
            this.LoadListView();
        }

        /// <summary>
        /// 加载菜单
        /// </summary>
        private void LoadListView()
        {
            ImageList image = new ImageList();
            image.ImageSize = new Size(1, 35);//设置每次点击view时以图片的形式
            ColumnHeader ch = new ColumnHeader();
            ch.Text = "功能菜单";
            ch.Width = splitContainer1.Panel1.Width;
            ch.TextAlign = HorizontalAlignment.Center;
            listView1.Columns.Add(ch);//设置listview的列名
            listView1.SmallImageList = image;//设置每个view的显示形式
            LoadListItem();//加载一级栏目
            listView1.Width = ch.Width;//设置每个view的宽度都一致
        }

        /// <summary>
        /// 一级菜单
        /// </summary>
        private void LoadListItem()
        {
            listView1.Items.Clear();//清空菜单
            //添加菜单
            ListViewItem AddCustomer = new ListViewItem("     添加客户");
            ListViewItem QueryCustomer = new ListViewItem("     查询客户信息");
            ListViewItem AddProject = new ListViewItem("     添加项目");
            ListViewItem QueryProject = new ListViewItem("     查询项目");
            ListViewItem AddOrder = new ListViewItem("     添加订单");
            ListViewItem QueryOrders = new ListViewItem("     查询订单");
            ListViewItem AddShipment = new ListViewItem("     添加发货");
            ListViewItem QueryShipment = new ListViewItem("     查询发货单");
            ListViewItem AddProductType = new ListViewItem("     添加产品类型");
            ListViewItem QueryProductType = new ListViewItem("     查询产品类型");
            ListViewItem QueryOrderItems = new ListViewItem("     查询数据");
            listView1.Items.Add(AddCustomer);
            listView1.Items.Add(QueryCustomer);
            listView1.Items.Add(AddProject);
            listView1.Items.Add(AddOrder);
            listView1.Items.Add(QueryOrders);
            listView1.Items.Add(AddShipment);
            listView1.Items.Add(AddProductType);
            listView1.Items.Add(QueryProductType);
            listView1.Items.Add(QueryOrderItems);
        }

        /// <summary>
        /// listview的鼠标单击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void listView1_MouseClick(object sender, MouseEventArgs e)
        {
            foreach (ListViewItem item in listView1.Items)
            {
                item.BackColor = Color.WhiteSmoke;//遍历每个菜单栏的颜色
            }
            if (e.Button == MouseButtons.Left)
            {
                if (listView1.SelectedItems.Count > 0)
                {
                    listView1.Items[listView1.FocusedItem.Index].BackColor = Color.LightGray;//设置选中菜单栏的颜色
                    string choose = listView1.Items[listView1.FocusedItem.Index].Text;//选中菜单栏的文本
                    ChangePlanel(choose.Trim());//根据文本名称进行相应的展示
                }
            }
        }

        /// <summary>
        /// 根据文本名称进行相应的展示
        /// </summary>
        /// <param name="name"></param>
        private void ChangePlanel(string name)
        {
            splitContainer1.Panel2.Controls.Clear();
            switch (name)
            {
                case "添加客户":
                    this.menuAddCustomer_Click(null, null);
                    break;
                case "查询客户信息":
                    this.menuQueryCustomer_Click(null, null);
                    break;
                case "添加项目":
                    this.menuAddProject_Click(null, null);
                    break;
                case "查询项目":
                    this.menuQueryProject_Click(null, null);
                    break;
                case "添加订单":
                    this.menuAddOrder_Click(null, null);
                    break;
                case "查询订单":
                    this.menuQueryOrder_Click(null, null);
                    break;
                case "添加发货":
                    this.menuImportInvoiceDataByOrder_Click(null, null);
                    break;
                case "查询发货单":
                    this.menuQueryInvoice_Click(null, null);
                    break;
                case "添加产品类型":
                    this.menuAddProductType_Click(null, null);
                    break;
                case "查询产品类型":
                    this.menuQueryProductType_Click(null, null);
                    break;
                case "查询数据":
                    if (this.frmQueryOrderItem == null || this.frmQueryOrderItem.IsDisposed)
                    {
                        this.frmQueryOrderItem = new FormQueryOrderItem();
                        this.frmQueryOrderItem.MdiParent = this;
                    }
                    splitContainer1.Panel2.Controls.Add(frmQueryOrderItem);
                    this.frmQueryOrderItem.Dock = DockStyle.Fill;
                    this.frmQueryOrderItem.Show();
                    this.frmQueryOrderItem.Activate();
                    break;
                default:
                    break;
            }
        }

        private void menuLogin_Click(object sender, EventArgs e)
        {
            if (!this.isSelfMachine)
            {
                MessageBox.Show("此计算机并未授权，此程序只能在指定的计算机上运行！\r\n\r\n请不要将此程序移动到其他计算机上运行", "授权提示！", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else if (LoginInfo.IsLogined)
            {
                MessageBox.Show("现在已经登陆，请注销之后再登陆", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else
            {
                //new FormLogin(this)
                //{
                //    ShowInTaskbar = false
                //}.ShowDialog();
                FormLogin login = new FormLogin(this);
                login.ShowDialog();
            }

           // this.LoginOperation();
        }

        private void menuLogout_Click(object sender, EventArgs e)
        {
            if (LoginInfo.IsLogined)
            {
                Operators operators = new Operators();
                bool flag = false;
                try
                {
                    flag = operators.DownLine(LoginInfo.OperatorID);
                }
                catch (SqlException ex)
                {
                    MessageBox.Show(ex.Message + "\r\n\r\n或者是连接数据库的通道出现故障，请检查！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                catch (Exception ex2)
                {
                    MessageBox.Show(ex2.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                operators.Dispose();
                if (!flag)
                {
                    MessageBox.Show("注销用户出了问题\r\n可能在下次登陆时会提示此用户已经登陆", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
            }
            LoginInfo.Clear();
            this.LogoutOperation();
        }

        private void menuClose_Click(object sender, EventArgs e)
        {
            base.Close();
        }

        public void LoginOperation()
        {
            this.toolStripBtnAddCustomer.Enabled = true;
            this.toolStripBtnQueryCustomer.Enabled = true;
            this.toolStripBtnAddProject.Enabled = true;
            this.toolStripBtnQueryProject.Enabled = true;
            this.toolStripBtnAddOrder.Enabled = true;
            this.toolStripBtnQueryOrders.Enabled = true;
            this.toolStripBtnAddShipment.Enabled = true;
            this.toolStripBtnQueryShipment.Enabled = true;
            this.toolStripBtnAddProductType.Enabled = true;
            this.toolStripBtnQueryProductType.Enabled = true;
            this.toolStripBtnQueryOrderItems.Enabled = true;
            this.menuManage.Enabled = true;
            this.menuOrder.Enabled = true;
            this.menuInvoice.Enabled = true;
            this.menuTool.Enabled = true;
            this.menuWindow.Enabled = true;
            this.statusLabelOperator.Text = string.Concat(new string[]
            {
                "操作员：",
                LoginInfo.OperatorName,
                "（",
                LoginInfo.Role.RoleName,
                "）"
            });
            LoginInfo.IsLogined = true;
            this.timer1_Tick(null, null);
            this.timer1.Enabled = true;
            this.timer1.Start();
        }

        public void LogoutOperation()
        {
            this.toolStripBtnAddCustomer.Enabled = false;
            this.toolStripBtnQueryCustomer.Enabled = false;
            this.toolStripBtnAddProject.Enabled = false;
            this.toolStripBtnQueryProject.Enabled = false;
            this.toolStripBtnAddOrder.Enabled = false;
            this.toolStripBtnQueryOrders.Enabled = false;
            this.toolStripBtnAddShipment.Enabled = false;
            this.toolStripBtnQueryShipment.Enabled = false;
            this.toolStripBtnAddProductType.Enabled = false;
            this.toolStripBtnQueryProductType.Enabled = false;
            this.toolStripBtnQueryOrderItems.Enabled = false;
            this.menuManage.Enabled = false;
            this.menuOrder.Enabled = false;
            this.menuInvoice.Enabled = false;
            this.menuTool.Enabled = false;
            this.menuWindow.Enabled = false;
            this.statusLabelOperator.Text = "未登陆！";
            Form[] mdiChildren = base.MdiChildren;
            for (int i = 0; i < mdiChildren.Length; i++)
            {
                Form form = mdiChildren[i];
                form.Close();
            }
            LoginInfo.Clear();
            this.taskbarNotifier3.Hide();
            this.timer1.Stop();
            this.timer1.Enabled = false;
        }

        private void menuAddCustomer_Click(object sender, EventArgs e)
        {
            if (!LoginInfo.Role[PowerName.Coustomer_Create])
            {
                MessageBox.Show("你没有操作这里的权限！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else
            {
                if (this.frmAddCustomer == null || this.frmAddCustomer.IsDisposed)
                {
                    this.frmAddCustomer = new FormAddCustomer();
                    this.frmAddCustomer.MdiParent = this;
                }
                this.splitContainer1.Panel2.Controls.Add(frmAddCustomer);
                this.frmAddCustomer.Dock = DockStyle.Fill;
                this.frmAddCustomer.Show();
                this.frmAddCustomer.Activate();
            }
        }

        private void menuQueryCustomer_Click(object sender, EventArgs e)
        {
            if (!LoginInfo.Role[PowerName.Coustomer_See])
            {
                MessageBox.Show("你没有操作这里的权限！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else
            {
                if (this.frmQueryCustomer == null || this.frmQueryCustomer.IsDisposed)
                {
                    this.frmQueryCustomer = new FormQueryCustomer();
                    this.frmQueryCustomer.MdiParent = this;
                }
                this.splitContainer1.Panel2.Controls.Add(frmQueryCustomer);
                this.frmQueryCustomer.Dock = DockStyle.Fill;
                this.frmQueryCustomer.Show();
                this.frmQueryCustomer.Activate();
            }
        }

        private void menuAddProject_Click(object sender, EventArgs e)
        {
            if (!LoginInfo.Role[PowerName.Project_Create])
            {
                MessageBox.Show("你没有操作这里的权限！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else
            {
                if (this.frmAddProject == null || this.frmAddProject.IsDisposed)
                {
                    this.frmAddProject = new FormAddProject();
                    this.frmAddProject.MdiParent = this;
                }
                this.splitContainer1.Panel2.Controls.Add(frmAddProject);
                this.frmAddProject.Dock = DockStyle.Fill;
                this.frmAddProject.Show();
                this.frmAddProject.Activate();
            }
        }

        private void menuQueryProject_Click(object sender, EventArgs e)
        {
            if (!LoginInfo.Role[PowerName.Project_See])
            {
                MessageBox.Show("你没有操作这里的权限！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else
            {
                if (this.frmQueryProject == null || this.frmQueryProject.IsDisposed)
                {
                    this.frmQueryProject = new FormQueryProject();
                    this.frmQueryProject.MdiParent = this;
                }
                this.splitContainer1.Panel2.Controls.Add(frmQueryProject);
                this.frmQueryProject.Dock = DockStyle.Fill;
                this.frmQueryProject.Show();
                this.frmQueryProject.Activate();
            }
        }

        private void menuAddShatter_Click(object sender, EventArgs e)
        {
            if (this.frmAddShatter == null || this.frmAddShatter.IsDisposed)
            {
                this.frmAddShatter = new FormAddShatter();
                this.frmAddShatter.MdiParent = this;
            }
            this.splitContainer1.Panel2.Controls.Add(frmAddShatter);
            this.frmAddShatter.Dock = DockStyle.Fill;
            this.frmAddShatter.Show();
            this.frmAddShatter.Activate();
        }

        private void menuQueryShatter_Click(object sender, EventArgs e)
        {
            if (this.frmQueryShatters == null || this.frmQueryShatters.IsDisposed)
            {
                this.frmQueryShatters = new FormQueryShatters();
                this.frmQueryShatters.MdiParent = this;
            }
            this.splitContainer1.Panel2.Controls.Add(frmQueryShatters);
            this.frmQueryShatters.Dock = DockStyle.Fill;
            this.frmQueryShatters.Show();
            this.frmQueryShatters.Activate();
        }

        private void menuAddProductType_Click(object sender, EventArgs e)
        {
            if (this.frmAddProductType == null || this.frmAddProductType.IsDisposed)
            {
                this.frmAddProductType = new FormAddProductType();
                this.frmAddProductType.MdiParent = this;
            }
            this.splitContainer1.Panel2.Controls.Add(frmAddProductType);
            this.frmAddProductType.Dock = DockStyle.Fill;
            this.frmAddProductType.Show();
            this.frmAddProductType.Activate();
        }

        private void menuQueryProductType_Click(object sender, EventArgs e)
        {
            if (this.frmQueryProductType == null || this.frmQueryProductType.IsDisposed)
            {
                this.frmQueryProductType = new FormQueryProductType();
                this.frmQueryProductType.MdiParent = this;
            }
            this.splitContainer1.Panel2.Controls.Add(frmQueryProductType);
            this.frmQueryProductType.Dock = DockStyle.Fill;
            this.frmQueryProductType.Show();
            this.frmQueryProductType.Activate();
        }

        private void menuAddOperator_Click(object sender, EventArgs e)
        {
            if (!LoginInfo.Role[PowerName.User_Create])
            {
                MessageBox.Show("你没有操作这里的权限！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else
            {
                if (this.frmAddOperator == null || this.frmAddOperator.IsDisposed)
                {
                    this.frmAddOperator = new FormAddOperator();
                    this.frmAddOperator.MdiParent = this;
                }
                this.splitContainer1.Panel2.Controls.Add(frmAddOperator);
                this.frmAddOperator.Dock = DockStyle.Fill;
                this.frmAddOperator.Show();
                this.frmAddOperator.Activate();
            }
        }

        private void menuQueryOperator_Click(object sender, EventArgs e)
        {
            if (!LoginInfo.Role[PowerName.User_See])
            {
                MessageBox.Show("你没有操作这里的权限！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else if (!LoginInfo.IsLogined)
            {
                MessageBox.Show("你还没有登陆，暂时还不能操作", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else
            {
                if (this.frmQueryOperator == null || this.frmQueryOperator.IsDisposed)
                {
                    this.frmQueryOperator = new FormQueryOperator();
                    this.frmQueryOperator.MdiParent = this;
                }
                this.splitContainer1.Panel2.Controls.Add(frmQueryOperator);
                this.frmQueryOperator.Dock = DockStyle.Fill;
                this.frmQueryOperator.Show();
                this.frmQueryOperator.Activate();
            }
        }

        private void menuShowPower_Click(object sender, EventArgs e)
        {
            string text = "";
            for (int i = 0; i < LoginInfo.Role.PowerList.Count; i++)
            {
                string text2 = text;
                text = string.Concat(new string[]
                {
                    text2,
                    LoginInfo.Role.PowerList[i].PowerItemID.ToString(),
                    "、",
                    LoginInfo.Role.PowerList[i].Description,
                    "\r\n"
                });
            }
            if (text != "")
            {
                MessageBox.Show(text, "权限列表", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            }
            else
            {
                MessageBox.Show("没有获得任何权限", "权限列表", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            }
        }

        private void menuUpdatePassword_Click(object sender, EventArgs e)
        {
            new FormUpdatePassword
            {
                ShowInTaskbar = false
            }.ShowDialog();
        }

        private void menuAddPayment_Click(object sender, EventArgs e)
        {
            if (!LoginInfo.Role[PowerName.Order_Payed])
            {
                MessageBox.Show("你没有操作这里的权限！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else
            {
                if (this.frmAddPayment == null || this.frmAddPayment.IsDisposed)
                {
                    this.frmAddPayment = new FormAddPayment();
                    this.frmAddPayment.MdiParent = this;
                }
                this.splitContainer1.Panel2.Controls.Add(frmAddPayment);
                this.frmAddPayment.Dock = DockStyle.Fill;
                this.frmAddPayment.Show();
                this.frmAddPayment.Activate();
            }
        }

        private void menuQueryPayment_Click(object sender, EventArgs e)
        {
            if (!LoginInfo.Role[PowerName.Order_Payed])
            {
                MessageBox.Show("你没有操作这里的权限！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else
            {
                if (this.frmQueryPayment == null || this.frmQueryPayment.IsDisposed)
                {
                    this.frmQueryPayment = new FormQueryPayment();
                    this.frmQueryPayment.MdiParent = this;
                }
                this.splitContainer1.Panel2.Controls.Add(frmQueryPayment);
                this.frmQueryPayment.Dock = DockStyle.Fill;
                this.frmQueryPayment.Show();
                this.frmQueryPayment.Activate();
            }
        }

        private void menuBalance_Click(object sender, EventArgs e)
        {
            if (!LoginInfo.Role[PowerName.Order_Payed])
            {
                MessageBox.Show("你没有操作这里的权限！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else
            {
                FormBalance formBalance = new FormBalance();
                formBalance.MdiParent = this;
                this.splitContainer1.Panel2.Controls.Add(formBalance);
                formBalance.Show();
                formBalance.Activate();
            }
        }

        private void menu查询欠款客户信息_Click(object sender, EventArgs e)
        {
            if (!LoginInfo.Role[PowerName.Order_Payed])
            {
                MessageBox.Show("你没有操作这里的权限！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else
            {
                FormQueryBalance formQueryBalance = new FormQueryBalance();
                formQueryBalance.MdiParent = this;
                this.splitContainer1.Panel2.Controls.Add(formQueryBalance);
                formQueryBalance.Show();
                formQueryBalance.Activate();
            }
        }

        private void menuAddStuff_Click(object sender, EventArgs e)
        {
            if (this.frmAddStuff == null || this.frmAddStuff.IsDisposed)
            {
                this.frmAddStuff = new FormAddStuff();
                this.frmAddStuff.MdiParent = this;
            }
            this.splitContainer1.Panel2.Controls.Add(frmAddStuff);
            this.frmAddStuff.Dock = DockStyle.Fill;
            this.frmAddStuff.Show();
            this.frmAddStuff.Activate();
        }

        private void menuQueryStuff_Click(object sender, EventArgs e)
        {
            if (this.frmQueryStuff == null || this.frmQueryStuff.IsDisposed)
            {
                this.frmQueryStuff = new FormQueryStuff();
                this.frmQueryStuff.MdiParent = this;
            }
            this.splitContainer1.Panel2.Controls.Add(frmQueryStuff);
            this.frmQueryStuff.Dock = DockStyle.Fill;
            this.frmQueryStuff.Show();
            this.frmQueryStuff.Activate();
        }

        private void menuDeleteDate_Click(object sender, EventArgs e)
        {
            if (LoginInfo.Role.RoleID != 0)
            {
                MessageBox.Show("只有管理员才能进行此操作！", "提示！", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else
            {
                string a = InputBox.ShowBox("请输入登陆的管理员密码......", "", '●');
                if (a != LoginInfo.Password)
                {
                    MessageBox.Show("你所输入的密码错误！", "提示！", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                else
                {
                    FormDeleteData formDeleteData = new FormDeleteData();
                    this.splitContainer1.Panel2.Controls.Add(formDeleteData);
                    formDeleteData.ShowDialog();
                }
            }
        }

        private void menuAddOrder_Click(object sender, EventArgs e)
        {
            if (!LoginInfo.Role[PowerName.Order_Create])
            {
                MessageBox.Show("你没有操作这里的权限！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else if (LoginInfo.IsLogined)
            {
                if (this.frmAddOrder == null || this.frmAddOrder.IsDisposed)
                {
                    this.frmAddOrder = new FormAddOrder();
                    this.frmAddOrder.MdiParent = this;
                }
                this.splitContainer1.Panel2.Controls.Add(frmAddOrder);
                this.frmAddOrder.Dock = DockStyle.Fill;
                this.frmAddOrder.Show();
                this.frmAddOrder.Activate();
            }
        }

        private void menuQueryOrder_Click(object sender, EventArgs e)
        {
            if (this.frmQueryOrders == null || this.frmQueryOrders.IsDisposed)
            {
                this.frmQueryOrders = new FormQueryOrders();
                this.frmQueryOrders.MdiParent = this;
            }
            this.splitContainer1.Panel2.Controls.Add(frmQueryOrders);
            this.frmQueryOrders.Dock = DockStyle.Fill;
            this.frmQueryOrders.Show();
            this.frmQueryOrders.Activate();
        }

        private void menuSetOrderFinished_Click(object sender, EventArgs e)
        {
            string text = InputBox.ShowBox("请输入你要入库的订单号...", "");
            if (text != null)
            {
                Orders orders = new Orders();
                bool flag = false;
                try
                {
                    flag = orders.FinishOrder(text);
                }
                catch (SqlException ex)
                {
                    if (ex.Number >= 50000)
                    {
                        MessageBox.Show(text + ": " + ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                    else
                    {
                        MessageBox.Show(ex.Message + "\r\n\r\n或者是连接数据库的通道出现故障，请检查！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                }
                catch (Exception ex2)
                {
                    MessageBox.Show(ex2.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                orders.Dispose();
                if (flag)
                {
                    MessageBox.Show("设置成功！", "Success", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                }
            }
        }

        private void menuAddInvoice_Click(object sender, EventArgs e)
        {
            if (!LoginInfo.Role[PowerName.Invoice_Create])
            {
                MessageBox.Show("你没有操作这里的权限！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else
            {
                if (this.frmAddInvoice == null || this.frmAddInvoice.IsDisposed)
                {
                    this.frmAddInvoice = new FormAddInvoice();
                    this.frmAddInvoice.MdiParent = this;
                }
                this.splitContainer1.Panel2.Controls.Add(frmAddInvoice);
                this.frmAddInvoice.Dock = DockStyle.Fill;
                this.frmAddInvoice.Show();
                this.frmAddInvoice.Activate();
            }
        }

        private void menuQueryInvoice_Click(object sender, EventArgs e)
        {
            if (this.frmQueryInvoices == null || this.frmQueryInvoices.IsDisposed)
            {
                this.frmQueryInvoices = new FormQueryInvoices();
                this.frmQueryInvoices.MdiParent = this;
            }
            this.splitContainer1.Panel2.Controls.Add(frmQueryInvoices);
            this.frmQueryInvoices.Dock = DockStyle.Fill;
            this.frmQueryInvoices.Show();
            this.frmQueryInvoices.Activate();
        }

        private void menuImportInvoiceDataByFile_Click(object sender, EventArgs e)
        {
            Hashtable hashtable = new Hashtable();
            this.openFileDialog1.Filter = "文本文件(*.txt)|*.txt";
            this.openFileDialog1.Title = "请选择你要导入的文件";
            this.openFileDialog1.FileName = "";
            if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    StreamReader streamReader = new StreamReader(File.Open(this.openFileDialog1.FileName, FileMode.Open), Encoding.Default);
                    this.openFileDialog1.Dispose();
                    int num = 0;
                    string text;
                    while ((text = streamReader.ReadLine()) != null)
                    {
                        num++;
                        if (text.Trim() != "")
                        {
                            string[] array = text.Split(new char[]
                            {
                                ','
                            });
                            if (array.Length != 2 || !Regex.IsMatch(array[1].Trim(), "^[0-9]{1,}$") || Convert.ToInt32(array[1].Trim()) <= 0)
                            {
                                MessageBox.Show("第" + num.ToString() + "行数据格式有问题", "错误！", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                                streamReader.Close();
                                return;
                            }
                            hashtable.Add(array[0].Trim(), array[1].Trim());
                        }
                    }
                    streamReader.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "错误！", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }
                if (hashtable.Count > 0)
                {
                    //new FormDataAnalyze(hashtable)
                    //{
                    //    MdiParent = this
                    //}.Show();
                    FormDataAnalyze formDataAnalyze = new FormDataAnalyze(hashtable);
                    formDataAnalyze.MdiParent = this;
                    this.splitContainer1.Panel2.Controls.Add(formDataAnalyze);
                    formDataAnalyze.Show();
                }
                else
                {
                    MessageBox.Show("没有从文件中得到数据", "提示！", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
            }
        }

        private void menuImportInvoiceDataByOrder_Click(object sender, EventArgs e)
        {
            string text = InputBox.ShowBox("请输入你要出库的订单号...", "");
            if (text != null)
            {
                OrderItems orderItems = new OrderItems();
                DataTable dataTable = null;
                try
                {
                    dataTable = orderItems.GetProductIDCollection(text.Trim());
                }
                catch (SqlException ex)
                {
                    MessageBox.Show(ex.Message + "\r\n\r\n或者是连接数据库的通道出现故障，请检查！\r\n\r\n获取出库信息可能出现了错误", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                catch (Exception ex2)
                {
                    MessageBox.Show(ex2.Message + "\r\n\r\n订单项信息可能没有读出来", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                orderItems.Dispose();
                if (dataTable != null)
                {
                    if (dataTable.Rows.Count > 0)
                    {
                        Hashtable hashtable = new Hashtable();
                        for (int i = 0; i < dataTable.Rows.Count; i++)
                        {
                            if (Convert.ToInt32(dataTable.Rows[i][1]) > Convert.ToInt32(dataTable.Rows[i][2]))
                            {
                                hashtable.Add(dataTable.Rows[i][0].ToString(), Convert.ToInt32(dataTable.Rows[i][1]) - Convert.ToInt32(dataTable.Rows[i][2]));
                            }
                        }
                        if (hashtable.Count > 0)
                        {
                            //new FormDataAnalyze(hashtable)
                            //{
                            //    MdiParent = this
                            //}.Show();
                            FormDataAnalyze formDataAnalyze = new FormDataAnalyze(hashtable);
                            this.splitContainer1.Panel2.Controls.Add(formDataAnalyze);
                            formDataAnalyze.Dock = DockStyle.Fill;
                            formDataAnalyze.Show();
                        }
                        else
                        {
                            MessageBox.Show("此订单的数据已经全部发货 ！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        }
                    }
                    else
                    {
                        MessageBox.Show("此订单号没有数据，或者是订单号输入错误，请检查 ！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                }
            }
        }

        private void menuOption_Click(object sender, EventArgs e)
        {
            FormOption formOption = new FormOption(this);
            formOption.ShowDialog();
        }

        private void menuWindowArrange_Click(object sender, EventArgs e)
        {
            base.LayoutMdi(MdiLayout.ArrangeIcons);
        }

        private void menuWindowCascade_Click(object sender, EventArgs e)
        {
            base.LayoutMdi(MdiLayout.Cascade);
        }

        private void menuWindowHorizontal_Click(object sender, EventArgs e)
        {
            base.LayoutMdi(MdiLayout.TileHorizontal);
        }

        private void menuWindowVertical_Click(object sender, EventArgs e)
        {
            base.LayoutMdi(MdiLayout.TileVertical);
        }

        private void menuCloseAllWindow_Click(object sender, EventArgs e)
        {
            Form[] mdiChildren = base.MdiChildren;
            for (int i = 0; i < mdiChildren.Length; i++)
            {
                Form form = mdiChildren[i];
                form.Close();
            }
        }

        private void menuAbout_Click(object sender, EventArgs e)
        {
          
        }

        private void toolStripBtnAddCustomer_Click(object sender, EventArgs e)
        {
            this.menuAddCustomer_Click(null, null);
        }

        private void toolStripBtnQueryCustomer_Click(object sender, EventArgs e)
        {
            this.menuQueryCustomer_Click(null, null);
        }

        private void toolStripBtnAddProject_Click(object sender, EventArgs e)
        {
            this.menuAddProject_Click(null, null);
        }

        private void toolStripBtnQueryProject_Click(object sender, EventArgs e)
        {
            this.menuQueryProject_Click(null, null);
        }

        private void toolStripBtnAddOrder_Click(object sender, EventArgs e)
        {
            this.menuAddOrder_Click(null, null);
        }

        private void toolStripBtnQueryOrders_Click(object sender, EventArgs e)
        {
            this.menuQueryOrder_Click(null, null);
        }

        private void toolStripBtnAddShipment_Click(object sender, EventArgs e)
        {
            this.menuImportInvoiceDataByOrder_Click(null, null);
        }

        private void toolStripBtnQueryShipment_Click(object sender, EventArgs e)
        {
            this.menuQueryInvoice_Click(null, null);
        }

        private void toolStripBtnAddProductType_Click(object sender, EventArgs e)
        {
            this.menuAddProductType_Click(null, null);
        }

        private void toolStripBtnQueryProductType_Click(object sender, EventArgs e)
        {
            this.menuQueryProductType_Click(null, null);
        }

        private void toolStripBtnQueryOrderItems_Click(object sender, EventArgs e)
        {
            if (this.frmQueryOrderItem == null || this.frmQueryOrderItem.IsDisposed)
            {
                this.frmQueryOrderItem = new FormQueryOrderItem();
                this.frmQueryOrderItem.MdiParent = this;
            }
            this.splitContainer1.Panel2.Controls.Add(frmQueryOrderItem);
            this.frmQueryOrderItem.Dock = DockStyle.Fill;
            this.frmQueryOrderItem.Show();
            this.frmQueryOrderItem.Activate();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            OrderCondition orderCondition = new OrderCondition();
            orderCondition.SpeakForDateStart = DateTime.Today.ToShortDateString() + " 00:00:00";
            orderCondition.SpeakForDateEnd = DateTime.Today.ToShortDateString() + " 23:59:59";
            orderCondition.Locked = 1;
            orderCondition.LabelPrinted = 0;
            Orders orders = new Orders();
            int num = 0;
            try
            {
                num = orders.GetNeverPrint(orderCondition);
            }
            catch
            {
            }
            orders.Dispose();
            if (num > 0)
            {
                this.taskbarNotifier3.CloseClickable = true;
                this.taskbarNotifier3.TitleClickable = true;
                this.taskbarNotifier3.ContentClickable = true;
                this.taskbarNotifier3.EnableSelectionRectangle = true;
                this.taskbarNotifier3.KeepVisibleOnMousOver = true;
                this.taskbarNotifier3.ReShowOnMouseOver = true;
                this.taskbarNotifier3.Show("订单通知", "今天有  " + num.ToString() + "个  新的订单没有打印标签，请核实！\r\n点击查看详情......", 500, 6000, 500);
            }
        }

        private void taskbarNotifier3_ContentClick(object sender, EventArgs e)
        {
            FormOrderList formOrderList = new FormOrderList(new OrderCondition
            {
                SpeakForDateStart = DateTime.Today.AddYears(-6).ToShortDateString() + " 00:00:00",
                SpeakForDateEnd = DateTime.Today.ToShortDateString() + " 23:59:59",
                LabelPrinted = 0
            });
            if (formOrderList != null && !formOrderList.IsDisposed)
            {
                formOrderList.MdiParent = this;
                this.splitContainer1.Panel2.Controls.Add(formOrderList);
                formOrderList.Show();
                formOrderList.Activate();
            }
        }
    }
}
